
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layuiAdmin 网站用户</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div class="layui-form-item">

        <div class="layui-inline">
          <label class="layui-form-label">用户名</label>
          <div class="layui-input-block">
            <input type="text" id="name" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>

    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button>
        <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
      </div>

      <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
      <script type="text/html" id="imgTpl">
        <img style="display: inline-block; width: 50%; height: 100%;" src= {{ d.avatar }}>
      </script>
      <script type="text/html" id="table-useradmin-webuser">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script>
  var json;
  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
    var $ = layui.$
            ,form = layui.form
            ,table = layui.table;

    //表格初始化
    table.render({
      elem: "#LAY-user-manage"
      , method: 'get'
      , request: {
        pageName: 'offset' //页码的参数名称，默认：page
        , limitName: 'limit' //每页数据量的参数名，默认：limit
      }
      , url: '/user'
      //, contentType: 'application/json'
      , where: {
        'name': $('#name').val()
      }
      , cols: [[
        {type: "checkbox", fixed: "left"}
        , {field: 'id', width: '15%', title: '编号'}
        , {field: 'name', width: '15%', title: '姓名'}
        , {field: 'age', width: '15%', title: '年龄'}
        , {field: 'email', title: '邮箱'}
        , {
          title: "操作",
          width: 150,
          align: "center",
          fixed: "right",
          toolbar: "#table-useradmin-webuser"
        }
      ]]
      , parseData: function (res) { //res 即为原始返回的数据
        console.log(res)
        return {
          "code": 0, //解析接口状态
          // "msg": res.msg, //解析提示文本
          "count": res.total, //解析数据长度
          "data": res.records //解析数据列表
        };
      }
      , even: true
      , page: !0
      , limit: 30
      , height: "full-220"
      , text: "对不起，加载出现异常！"
    });


    table.on('tool(LAY-user-manage)', function (obj) {
      let data = obj.data;
      if (obj.event === 'edit') {
        json = JSON.stringify(data);
        layer.open({
          type: 2,
          title: "编辑用户",
          content: "../../../views/user/user/userform.html",
          maxmin: !0,
          area: ["500px", "450px"],
          btn: ["确定", "取消"],
          yes: function (e, t) {
            var l = window["layui-layer-iframe" + e], r = "LAY-user-front-submit",
                    n = t.find("iframe").contents().find("#" + r);
            l.layui.form.on("submit(" + r + ")", function (t) {
              t.field;

              $.ajax({
                type: 'PUT',
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                url: '/user',
                data: JSON.stringify(t.field),
                success: function(data){
                  if (data) {
                    layer.msg('编辑成功');

                  } else {
                    layer.msg('编辑失败');
                  }
                  table.reload('LAY-user-manage');
                  layer.close(e)
                },
                error: function(msg){
                  alert("提交失败!");
                }
              });


            }), n.trigger("click")
          },
          success: function (e, t) {
          }
        })
      } else if (obj.event === 'del') {
        layer.confirm('确定删除选中数据？', function (index) {
          $.ajax({
            url:  "/user/" + data.id,
            type: "DELETE",
            // contentType: 'application/json',
            success: function (data) {
              if (data) {
                layer.msg('删除成功');
              } else {
                layer.msg('删除失败');
              }
              table.reload('LAY-user-manage'); //数据刷新
              layer.close(index) //关闭弹层
            }
          });
        });
      }
    });

    //监听搜索
    form.on('submit(LAY-user-front-search)', function(data){
      var field = data.field;

      //执行重载
      table.reload('LAY-user-manage', {
        where: field
      });
    });

    //事件
    var active = {
      batchdel: function(){
        var checkStatus = table.checkStatus('LAY-user-manage')
                ,checkData = checkStatus.data; //得到选中的数据

        if(checkData.length === 0){
          return layer.msg('请选择数据');
        }

        layer.confirm('确定删除吗？', function(index) {

          //执行 Ajax 后重载
          /*
          admin.req({
            url: 'xxx'
            //,……
          });
          */
          $.ajax({
            url:  "/user",
            type: "DELETE",
            data: JSON.stringify(checkData),
            contentType: 'application/json',
            success: function (data) {
              if (data) {
                layer.msg('删除成功');
              } else {
                layer.msg('删除失败');
              }
              table.reload('LAY-user-manage'); //数据刷新
              layer.close(index) //关闭弹层
            }
          });
        });
      }
      ,add: function(){
        layer.open({
          type: 2
          ,title: '添加用户'
          ,content: 'userform-add.html'
          ,maxmin: true
          ,area: ['500px', '450px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submitID = 'LAY-user-front-submit'
                    ,submit = layero.find('iframe').contents().find('#'+ submitID);

            //监听提交
            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，静态更新表格中的数据
              //$.ajax({});

              $.ajax({
                type: 'POST',
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                url: '/user',
                data: JSON.stringify(field),
                success: function(data){
                  if (data) {
                    layer.msg('编辑成功');

                  } else {
                    layer.msg('编辑失败');
                  }
                  table.reload('LAY-user-manage'); //数据刷新
                  layer.close(index) //关闭弹层
                },
                error: function(msg){
                  alert("提交失败!");
                }
              });
            });

            submit.trigger('click');
          }
        });
      }
    };

    $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
</script>
</body>
</html>